﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.Sellers;
namespace ZBJF.Warranty.BLL
{
    public partial class SellersService
    {
        public IQueryable<Sellers> GetPagingList(SellersModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<Sellers>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                string kw = model.Keyword.ToLower();
                expressionWhere = expressionWhere.And(o => o.SellersName.ToLower().Contains(kw));
            }
            if (model.SellersTypeID.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.SellersType == model.SellersTypeID);
            }
            if (model.PareSellersOrganizationId.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.SellersOrganization.ParentOrganizationId == model.PareSellersOrganizationId);
            }
            if (model.SellersOrganizationId.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.SellersOrganizationId == model.SellersOrganizationId);
            }
            if (!string.IsNullOrWhiteSpace(model.SellersName))
            {
                expressionWhere = expressionWhere.And(o => o.SellersName.StartsWith(model.SellersName));
            }
            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "CreateTime desc";
            }
            return LoadPagerEntities<Sellers>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        public bool IsExist(string sellerCode)
        {
            var expressionWhere = PredicateExtensions.True<Sellers>();
            expressionWhere = expressionWhere.And(o => o.SellerCode.Equals(sellerCode));
            return LoadEntities(expressionWhere).Any();
        }
        public bool IsExist(string sellerName, int sellersType, int sellersOrganizationId)
        {
            var expressionWhere = PredicateExtensions.True<Sellers>();
                expressionWhere = expressionWhere.And(o => o.SellersName.Equals(sellerName));
            if (sellersType > 0)
                expressionWhere = expressionWhere.And(o => o.SellersType == sellersType);
            if (sellersOrganizationId > 0)
                expressionWhere = expressionWhere.And(o => o.SellersOrganizationId == sellersOrganizationId);
            return LoadEntities(expressionWhere).Any();
        }
        public Sellers LoadByID(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public Sellers LoadBySellerCode(string sellerCode)
        {
            return LoadEntities(o => o.SellerCode.Equals(sellerCode)).FirstOrDefault();
        }
        /// <summary>
        /// 获取销售商集合
        /// </summary>
        /// <returns></returns>
        public IQueryable<Sellers> GetSellersAll()
        {
            return LoadEntities(o => true);
        }

        #region 获取销售商密码 + string GetSellerPassword(string sellerCode)
        /// <summary>
        /// 获取销售商的密码
        /// </summary>
        /// <param name="sellerCode">销售商编码</param>
        /// <returns></returns>
        public string GetSellerPassword(string sellerCode)
        {
            if (string.IsNullOrEmpty(sellerCode))
                return "";
            Sellers seller= LoadEntities(o => o.SellerCode.Equals(sellerCode)).FirstOrDefault();
            if (seller == null)
                return "";

            return seller.SellerPassword;
        }
        #endregion

    }
}
